<?php
/**
 * @see Epic_Db_Collection_Abstract
 */
require 'Epic/Db/Collection/Abstract.php';

/**
 * Objects store a set of objects returned from the database
 *
 * @category   Epic
 * @package    Epic_Db
 * @author     Epic Core Team <rukanhitokiri@gmail.com>
 */
class Epic_Db_Collection extends Epic_Db_Collection_Abstract
{
   	/**
	 * Name table
	 * @var string
	 */
	protected $_nameTable;
    
	/**
	 * Key field primary name
	 * @var string
	 */
	protected $_idFieldName = 'id';
	
	/**
     * Adapter instance
     * @var Epic_Db_Adapter_Abstract
     */
    protected $_adapter;
    
    /**
     * Query object
     * @var Epic_Db_Select
     */
    protected $_select;
    
    /**
     * Contructor
     * 
     * @param Epic_Db_Adapter_Interface $adapter
     * @param unknown_type $nameTable
     * @param unknown_type $idFieldName
     */
    public function __construct(Epic_Db_Adapter_Interface $adapter, $nameTable = '', $idFieldName = '')
    {
    	$this->_adapter = $adapter;
    	$this->_nameTable = $nameTable;
    	$this->_idFieldName = $idFieldName;
    }
    
    /**
     * Retrieve adapter instance
     *
     * @return Epic_Db_Adapter_Interface
     */
    public function getAdapter()
    {
    	return $this->_adapter;
    }
    
    /**
     * Retrieve Zend_Db_Select object
     *
     * @return Epic_Db_Select a query object
     */
    public  function getSelect()
    {
    	if (is_null($this->_select)) {
    		$this->_select = new Epic_Db_Select();
    		$this->_select->from($this->_nameTable);
    	}
    	return $this->_select;
    }
    
    /**
     * Filter the data indicated by conditions
     *
     * @param string $field
     * @param array $condition
     * @return Epic_Db_Data_Collection_Abstract
     */
    public function where($field, $condition)
    {
    	$this->getSelect()->where($field, $condition);
    	return $this;
    }
	
	public function groupBy($field)
    {
    	$this->getSelect()->groupBy($field);
    	return $this;
    }
	
    /**
     * Indicate fileds received
     *
     * @param  string|array $fields
     * @return Epic_Db_Data_Collection_Abstract
     */
    public function field($fields)
    {
    	$this->getSelect()->field($fields);
    	return $this;
    }
    
    
    
    
    
    
    
    
    /**
     * Tải dữ liệu về cho collection
     *
     * @return null|Epic_Db_Data_Collection_Abstract
     */
    public function load()
    {
    	if (is_null($this->_select))
    		$this->getAdapter()->select($this->_nameTable);
    	else 
    		$this->getAdapter()->query($this->getSelect()->select());
    	
    	$rows = $this->getAdapter()->fetchAll();
    	
    	// Add rows (instance Epic_Db_Model_Object) to collection
    	foreach ($rows as $row) {
    		$obj = new Epic_Db_Model_Object($row);
    		if($this->_idFieldName)
    			$obj->setIdFieldName($this->_idFieldName);
    		$this->add($obj);
    	}
    	return $this->count() !== 0 ? $this : null;
    }
    
    /**
     * Kết nối thêm bảng
     *
     * @param  string $table
     * @param  string $on
     * @param  array $cond
     * @return Epic_Db_Data_Collection_Abstract
     */
    public function join($table, $on, $cond)
    {
    	$this->getSelect()->join($table, $on, $cond);
    	return $this;
    }
    
    /**
     * Kết nối thêm bảng
     *
     * @param  string $table
     * @param  string $on
     * @param  array $cond
     * @return Epic_Db_Data_Collection_Abstract
     */
    public function leftJoin($table, $on, $cond)
    {
    	$this->getSelect()->leftJoin($table, $on, $cond);
    	return $this;
    }
    
    /**
     * Thêm lựa chọn thứ tự trả về
     *
     * @param  string $field
     * @param  string $direction
     * @return Epic_Db_Data_Collection_Abstract
     */
    public function order($field, $direction = 'DESC')
    {
    	$this->getSelect()->order($field, $direction);
    	return $this;
    }
    
    /**
     * Thiết lập giới hạn số kết quả trả về.
     *
     * @param int $count OPTIONAL số hàng trả về.
     * @param int $offset OPTIONAL bắt đầu trả lại từ hàng thứ bao nhiêu.
     * @return Epic_Db_Data_Collection_Abstract
     */
    public function limit($count = null, $offset = 0)
    {
    	$this->getSelect()->limit($count, $offset);
    	return $this;
    }
}